var app = getApp();
const useful_function = require('../../utils/usefulfunction.js');
Page({

    /**
     * 页面的初始数据
     */
    data: {
        group_id: null,  //群id
        group_name: null,  //群昵称
        avatar: null,  //群头像
        detail: null,  //简介
        detail_isfold: true, //20230106新增变量
        created_time: null,  //创建时间
        isManager: false,  //判断是否为用户自己管理的群组
        isJoin: false
    },

    //20230106增加函数：查看群成员
    goToMember: function (event) {
        wx.navigateTo({
            url: `/pages/group/groupmember?gid=${event.currentTarget.dataset.info[0]}&isManager=${event.currentTarget.dataset.info[1]}`,
        });
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        this.setData({ 'group_id': parseInt(options.gid) });
        this.setData({ 'isManager': Boolean(options.isManager) });
        this.setData({ 'isJoin': Boolean(options.isJoin) });
        this.getGroupInfo();
    },

    getGroupInfo: function () {
        var that = this;
        wx.request({
            url: app.buildUrl("/group/info"),
            header: app.getRequestHeader(),
            data: {
                "group_id": this.data.group_id
            },
            success: function (res) {
                var resp = res.data;
                if (resp.code != 200) {
                    app.alert({
                        "content": resp.msg
                    });
                    return;
                }
                var group_info = resp.data.group_info;
                that.setData({
                    avatar: group_info.avatar,
                    group_name: group_info.name,
                    detail: group_info.detail,
                    created_time: useful_function.getDate(group_info.created_time)
                });
            }
        })
    },

    joinGroup: function() {
        var that = this;
        var manager_list = wx.getStorageSync('manager_group_data_list');
        var member_list = wx.getStorageSync('member_group_data_list');
        var group_list = manager_list.concat(member_list);
        var join_gid = this.data.group_id;
        for (var i = 0; i < group_list.length; i++) {
            if (join_gid == group_list[i].gid) {
                app.alert({"content": "你已加入该群"});
                return;
            }
        }
        wx.request({
          url: app.buildUrl('/group/join-request'),
          header: app.getRequestHeader(),
          data: {
            'user_id': app.globalData.userID,
            'group_id': that.data.group_id
          },
          success: function(res) {
            var resp = res.data;
            app.alert({
                "content": resp.msg
            });
            if (resp.code == 200) {
                setTimeout(function(){
                    wx.switchTab({
                        url: '/pages/function/function'
                    });
                }, 2000);
            }
          }
        })
    },

    goToSet: function (event) {
        wx.navigateTo({
            url: `/pages/function/group_create?name=${event.currentTarget.dataset.info[0]}&detail=${event.currentTarget.dataset.info[1]}&gid=${event.currentTarget.dataset.info[2]}`
        });
    },

    setavatar(e){
        if (this.data.isManager) {
            wx.navigateTo({
                url: `/pages/group/groupavatar?obj=${e.currentTarget.dataset.obj[0]}&gidd=${e.currentTarget.dataset.obj[1]}&ism=${e.currentTarget.dataset.obj[2]}`
              })
        }
    },

    deleteGroup: function() {
        var that = this;
        app.alert({
            "content": "确定要解散该群吗",
            "showCancel": true,
            // 点击确定执行
            "cb_confirm": function() {
                wx.request({
                  url: app.buildUrl('/group/remove-group'),
                  header: app.getRequestHeader(),
                  data: {
                      group_id: that.data.group_id
                  },
                  success: function(res) {
                    var resp = res.data;
                    app.alert({
                        "content": resp.msg
                    });
                    if (resp.code == 200) {
                        setTimeout(function(){
                            wx.switchTab({
                                url: '/pages/group/group'
                            });
                        }, 2000);
                    }
                  }
                })
            },
        })
    },

    quit: function() {
        var that = this;
        app.alert({
            "content": "确定要退出该群吗",
            "showCancel": true,
            "cb_confirm": function() {
                wx.request({
                  url: app.buildUrl('/group/quit-group'),
                  header: app.getRequestHeader(),
                  data: {
                      user_id: app.globalData.userID,
                      group_id: that.data.group_id
                  },
                  success: function(res) {
                    var resp = res.data;
                    app.alert({
                        "content": resp.msg
                    });
                    if (resp.code == 200) {
                        setTimeout(function(){
                            wx.switchTab({
                                url: '/pages/group/group'
                            });
                        }, 2000);
                    }
                  }
                })
            },
        })
    },

    goToInvite: function() {
        wx.navigateTo({
            url: "/pages/function/group_join?action=invite&group_id=" + this.data.group_id
        });
    },

    //20230106增加函数：折叠
    activeFold: function () {
        this.setData({
            detail_isfold: !this.data.detail_isfold
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})
